<?php
class RoleAction extends Action {
	function _initialize() {
		$this->assign ( "menuFirst", session ( 'menuFirst' ) );
		$this->assign ( "menuSecond", session ( 'menuSecond' ) );
	}
	function rlist() {
		if (! empty ( $_GET ['rolename'] )) {
			$sql [] = array (
					'rolename' => array (
							'like',
							"%{$_GET['rolename']}%" 
					) 
			);
		}
		
		$ob = M ( "csl_role" );
		$cpp = 30; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $ob->where ( $sql )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
		
		$list = $ob->where ( $sql )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		
		$this->assign ( "list", $list );
		$this->assign ( 'show', $show ); // 赋值分页输出
		$this->display ( "Role/rlist" );
	}
	function detail() {
		if ($_GET ['opt'] == 'add') { // 新增跳转
			$ob = M ( "csl_menu" );
			// 查询第一级菜单
			$menulist = $ob->where ( array (
					"pid" => "0" 
			) )->field ( "menuid,name,pid,module,action,data,remark,ordid,display" )->select ();
			if (empty ( $menulist )) {
				$this->error ( '没有菜单，请添加！', '/Admin/Role/rlist' );
				return;
			}
			$tree = $this->getMenuTree ( $menulist ); // 菜单树
			
			$role = M ( "csl_role" );
			$rlist = $role->select ();
			
			$this->assign ( "tree", $tree );
			$this->assign ( "rlist", $rlist );
			$this->display ( "Role/detail" );
			return;
		}
		
		if ($_GET ["opt"] == 'edit') { // 编辑
			$ob = M ( "csl_menu" );
			
			// 查询第一级菜单
			$menulist = $ob->where ( array (
					"pid" => "0" 
			) )->field ( "menuid,name,pid,module,action,data,remark,ordid,display" )->select ();
			if (empty ( $menulist )) {
				$this->error ( '没有菜单，请添加！', '/Admin/Role/rlist' );
				return;
			}
			$tree = $this->getMenuTree ( $menulist ); // 菜单树
			
			$ob = M ( "csl_role_auth" );
			$authlist = $ob->where ( array (
					"roleid" => $_GET ["roleid"] 
			) )->select (); // 角色、菜单
			
			$ob = M ( "csl_role" );
			$info = $ob->where ( array (
					"roleid" => $_GET ["roleid"] 
			) )->find (); // 角色
			                                                              
			// $ob = M("csl_role");
			                                                              // $rlist = $ob->select(); //角色的上级
			
			$this->assign ( "tree", $tree );
			$this->assign ( "modify", '1' );
			$this->assign ( "info", $info );
			// $this->assign("rlist", $rlist);
			$this->assign ( "authlist", json_encode ( $authlist ) ); // php数组转换json数组
			$this->display ( 'Role/detail' );
			return;
		}
		
		if ($_POST) {
			
			// 添加
			$ob = M ( "csl_role" );
			$roleid = '';
			if ($_POST ['roleid'] != '') {
				$roleid = $_POST ['roleid'];
				$ob->where ( array (
						'roleid' => $_POST ['roleid'] 
				) )->save ( $_POST );
			} else {
				$roleid = $ob->add ( $_POST );
			}
			
			// 菜单不为空
			if (! empty ( $_POST ['menuIds'] )) {
				$ids = substr ( $_POST ['menuIds'], 0, strlen ( $_POST ['menuIds'] ) - 1 );
				$menuIds = explode ( ",", $ids );
				$auth = M ( 'csl_role_auth' );
				
				if ($_POST ['roleid'] != '') {
					$auth->where ( array (
							"roleid" => $_POST ['roleid'] 
					) )->delete ();
				}
				
				foreach ( $menuIds as $key => $value ) {
					$data ['roleid'] = $roleid;
					$data ['menuid'] = $value;
					$auth->add ( $data );
				}
			}
			
			$this->redirect ( '/Admin/Role/rlist/' );
			return;
		}
	}
	function getMenuTree($menulist) {
		$treeStr = $treeStr . " [";
		foreach ( $menulist as $key => $value ) {
			// 父类
			$treeStr = $treeStr . " { id:" . $value ['menuid'] . ", pId:" . $value ['pid'] . ", name:'" . $value ['name'] . "', open:false},";
			
			$ob = M ( "csl_menu" );
			$listTwo = $ob->where ( array (
					"pid" => $value ["menuid"] 
			) )->select ();
			if (! empty ( $listTwo )) {
				foreach ( $listTwo as $key2 => $value2 ) {
					// 第二级
					$treeStr = $treeStr . "{ id:" . $value2 ['menuid'] . ", pId:" . $value2 ['pid'] . ", name:'" . $value2 ['name'] . "'},";
				}
			}
		}
		
		return substr ( $treeStr, 0, strlen ( $treeStr ) - 1 ) . "];";
	}
}

?>